﻿/****************************** 模块头 ******************************\
* 模块名:  JobFlow.cs
* 项目名:  DingTalkAPIHelp
* 版权(c)  auth markeluo.
* 
*说明： JobFlow 用于调用钉钉API 审批相关接口
* This source is subject to the markeluo
* All other rights reserved.
\***************************************************************************/

using System.Collections.Generic;
using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;
using DingTalkAPIHelp.Base;

namespace DingTalkAPIHelp.Flow
{
    /// <summary>
    /// 工作审批
    /// </summary>
    public class JobFlowManager:SysConfigManager
    {
        private static JobFlowManager manager = new JobFlowManager();
        /// <summary>
        /// 审批管理实例(单例)
        /// </summary>
        public static JobFlowManager Instance
        {
            get
            {
                return manager;
            }
        }

        /// <summary>
        /// 1.获取用户可见的审批模板列表
        /// </summary>
        /// <param name="UserId">用户id,可选，若不传则查询企业内所有模板</param>
        /// <param name="Offset">分页游标，从0开始</param>
        /// <param name="Size">分页大小，最大可设置成100</param>
        public OapiProcessListbyuseridResponse FlowModelList(string UserId, long Offset, long Size)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/process/listbyuserid");
            OapiProcessListbyuseridRequest request = new OapiProcessListbyuseridRequest();
            request.Userid = UserId;
            request.Offset = Offset;
            request.Size = Size;
            OapiProcessListbyuseridResponse response = client.Execute(request, accessToken);
            return response;
        }

        /// <summary>
        /// 2.发起审批实例
        /// </summary>
        /// <param name="ProcessCode">审批模板流Code</param>
        /// <param name="formdata">审批表单数据</param>
        /// <param name="Approvers">审批人(按照审批顺序多个逗号分隔)</param>
        /// <param name="OriginatorUserId">发起人</param>
        /// <param name="DeptId">发起人部门</param>
        /// <param name="CcList">抄送人(多个逗号分隔)</param>
        /// <returns></returns>
        public OapiProcessinstanceCreateResponse FlowProcessSend(string ProcessCode, List<FlowFormItem> formdata, string Approvers,
            string OriginatorUserId, long DeptId, string CcList)
        {
            DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/create");
            OapiProcessinstanceCreateRequest request = new OapiProcessinstanceCreateRequest();
            request.AgentId = AgentId;//应用agentid (应用需要到工作台 https://oa.dingtalk.com/index.htm#/microApp/microAppList 下创建)
            request.ProcessCode = ProcessCode;//审批流唯一码(流程模板id->到工作台-审批-编辑模板页面的url中复制)

            //表单
            List<OapiProcessinstanceCreateRequest.FormComponentValueVoDomain> formComponentValues = new List<OapiProcessinstanceCreateRequest.FormComponentValueVoDomain>();
            if (formdata != null && formdata.Count > 0)
            {
                OapiProcessinstanceCreateRequest.FormComponentValueVoDomain votemp = null;
                foreach (FlowFormItem keyitem in formdata)
                {
                    votemp = new OapiProcessinstanceCreateRequest.FormComponentValueVoDomain();
                    votemp.Name = keyitem.Name;
                    votemp.Value = keyitem.Value;
                    formComponentValues.Add(votemp);
                }
            }
            request.FormComponentValues_ = formComponentValues;

            request.Approvers = Approvers;// "1851605243858016,manager4990";//审批人userid列表,逗号隔开
            request.OriginatorUserId = OriginatorUserId;//审批发起人userid
            request.DeptId = DeptId;//发起人所在部门id,49122289，电脑部
            request.CcList = CcList;//抄送人userid列表,逗号隔开
            request.CcPosition = "START";//抄送时间,分为：START,FINISH,START_FINISH;
            OapiProcessinstanceCreateResponse response = client.Execute(request, accessToken);
            return response;
        }

        /// <summary>
        /// 3.获取用户待审批数量
        /// </summary>
        /// <param name="UserId">用户Id</param>
        public OapiProcessGettodonumResponse FlowProcessTodoNum(string UserId)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/process/gettodonum");
            OapiProcessGettodonumRequest req = new OapiProcessGettodonumRequest();
            req.Userid = UserId;
            OapiProcessGettodonumResponse rsp = client.Execute(req, accessToken);
            return rsp;
        }

        /// <summary>
        /// 4.批量获取审批实例id列表
        /// </summary>
        /// <param name="ProcessCode">审批模板流Code,必须</param>
        /// <param name="StartTime">开始时间,必须</param>
        /// <param name="EndTime">结束时间,可选</param>
        /// <param name="Size">分页参数，每页大小，最多传10,可选</param>
        /// <param name="Cursor">分页查询的游标，最开始传0,可选</param>
        /// <param name="UserIdList">发起人用户id列表，最大列表长度：20,可选</param>
        /// <returns></returns>
        public OapiProcessinstanceListidsResponse FlowProcessInstanceIds(string ProcessCode, long StartTime, long EndTime, long Size, long Cursor, string UserIdList)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/listids");
            OapiProcessinstanceListidsRequest req = new OapiProcessinstanceListidsRequest();
            req.ProcessCode = ProcessCode;
            req.StartTime = StartTime;
            if (!LongIsNull(EndTime))
            {
                req.EndTime = EndTime;
            }
            if (!LongIsNull(Size))
            {
                req.Size = Size;
            }
            if (!LongIsNull(Cursor))
            {
                req.Cursor = Cursor;
            }

            if (!string.IsNullOrWhiteSpace(UserIdList))
            {
                req.UseridList = UserIdList;
            }
            OapiProcessinstanceListidsResponse response = client.Execute(req, accessToken);
            return response;
        }

        /// <summary>
        /// 5.获取单个审批实例详情
        /// </summary>
        /// <param name="InstanceId">审批实例id</param>
        /// <returns></returns>
        public OapiProcessinstanceGetResponse FlowProcessInstance(string InstanceId)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/processinstance/get");
            OapiProcessinstanceGetRequest request = new OapiProcessinstanceGetRequest();
            request.ProcessInstanceId = InstanceId;
            OapiProcessinstanceGetResponse response = client.Execute(request, accessToken);
            return response;
        }

        private bool LongIsNull(long _value)
        {
            if (_value > 0)
            {
                return false;
            }
            return true;
        }
    }
}
